﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Objects;

namespace Harmonic.ReadModel
{
    public class Queries
    {
        public static bool UsernameExists(string Username)
        {
            using (ReadModel readModel = new ReadModel())
            {
                var users = from u in readModel.Users
                            where u.Username == Username
                            select u.Username;

                return users.Count() > 0 ? true : false;
            }
        }

        public static IList<User> GetUsers()
        {
            using (Harmonic.ReadModel.ReadModel readModel = new Harmonic.ReadModel.ReadModel())
            {

                //var users = from user in readModel.Users
                //            select new { user.Id, user.Username, user.State, user.Email, user.Version };

                //readModel.Users.All();

                return readModel.Users.ToList<User>();
            }
        }

        public IList<Product> GetAllProducts()
        {
            using (Harmonic.ReadModel.ReadModel readModel = new Harmonic.ReadModel.ReadModel())
            {
                return readModel.Products.ToList<Product>();
            }
        }

        public Product SelectProduct(int id)
        {
            using (Harmonic.ReadModel.ReadModel readModel = 
                new Harmonic.ReadModel.ReadModel())
            {
                return readModel.Products.FirstOrDefault(p => p.ProductId == id);
            }
        }
    }
}
